import time
import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class TestPod:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v25.06/resourcemanage/pod/1_get_pod_list.yaml'))
    def test_get_pod_list(self, podinfo):
        # 获取pod列表接口-正常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default')
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v25.06/resourcemanage/pod/2_create_pod.yaml'))
    def test_create_pod(self, podinfo):
        # 创建pod接口-正常
        base_url = datas.baseUrl + TestPod.url_unify
        if '{namespace}' in podinfo['request']['url']:
            url = base_url + podinfo['request']['url'].replace('{namespace}', 'default')
        else:
            url = base_url + podinfo['request']['url']
        method = podinfo['request']['method']
        data = podinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        podname = req.json()['metadata']['name']
        globals()['podname'] = podname
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/pod/3_get_specific_pod.yaml'))
    def test_get_specific_pod(self, podinfo):
        # 获取指定pod接口-正常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default').replace('{podname}',
                                                                                             globals()['podname'])
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v25.06/resourcemanage/pod/4_update_pod.yaml'))
    def test_update_pod(self, podinfo):
        # 更新指定pod接口-正常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default').replace('{podname}',
                                                                                             globals()['podname'])
        method = podinfo['request']['method']
        podinfo['request']['data'] = globals()['updatereqbody']
        data = podinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/pod/5_update_section_pod.yaml'))
    def test_update_section_pod(self, podinfo):
        # 部分更新指定pod的接口-正常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default').replace('{podname}',
                                                                                             globals()['podname'])
        method = podinfo['request']['method']
        data = podinfo['request']['data']
        header = podinfo['request']['headers']
        if 'Content-Type' in header:
            req = RequestUtil().send_request(method, url, headers=header, json=data)
        else:
            req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/pod/6_delete_specific_pod.yaml'))
    def test_delete_specific_pod(self, podinfo):
        # 删除pod接口-正常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default').replace('{podname}',
                                                                                             globals()['podname'])
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/pod/1_get_pod_list_exception.yaml'))
    def test_get_pod_list_exception(self, podinfo):
        # 获取pod列表接口-异常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default')
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/pod/2_create_pod_exception.yaml'))
    def test_create_pod_exception(self, podinfo):
        # 创建pod接口-异常
        base_url = datas.baseUrl + TestPod.url_unify
        if '{namespace}' in podinfo['request']['url']:
            url = base_url + podinfo['request']['url'].replace('{namespace}', 'default')
        else:
            url = base_url + podinfo['request']['url']
        method = podinfo['request']['method']
        data = podinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/pod/3_get_specific_pod_exception.yaml'))
    def test_get_specific_pod_exception(self, podinfo):
        # 获取指定pod接口-异常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default').replace('{podname}',
                                                                                             globals()['podname'])
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/pod/4_update_pod_exception.yaml'))
    def test_update_pod_exception(self, podinfo):
        # 更新指定pod接口-异常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default').replace('{podname}',
                                                                                             globals()['podname'])
        method = podinfo['request']['method']
        podinfo['request']['data'] = globals()['updatereqbody']
        data = podinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/pod/5_update_section_pod_exception.yaml'))
    def test_update_section_pod_exception(self, podinfo):
        # 部分更新指定pod的接口-异常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default').replace('{podname}',
                                                                                             globals()['podname'])
        method = podinfo['request']['method']
        data = podinfo['request']['data']
        header = podinfo['request']['headers']
        if 'Content-Type' in header:
            req = RequestUtil().send_request(method, url, headers=header, json=data)
        else:
            req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.pod
    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/pod/6_delete_specific_pod_exception.yaml'))
    def test_delete_specific_pod_exception(self, podinfo):
        # 删除pod接口-异常
        base_url = datas.baseUrl + TestPod.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default').replace('{podname}',
                                                                                             globals()['podname'])
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']
        time.sleep(10)  # 确保pod删除完成，否则连续运行时容易报错
